package zjc.DeviceManage.service.Imp;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import zjc.DeviceManage.fragment.InformationFragment;
import zjc.DeviceManage.model.InformationList;
import zjc.DeviceManage.util.MyAppliacation;
import zjc.DeviceManage.util.MyHttpUtil;

/**
 * 作者：zxl on 2020/7/16 22:55
 * 邮箱：510252916@qq.com
 */
public class InformationServiceImp {
    private InformationList informationListFromJson;
    private void parseJSONtoInformationList(String responseData)  {

        Gson gson=new Gson();

        informationListFromJson = gson.fromJson(responseData,

                new TypeToken<InformationList>(){}.getType());

        Log.i("zjc",informationListFromJson.toString());

    }
    // 添加回调类

    private InformationFragment informationFragment;

// 注入回调类的构造函数

    public InformationServiceImp(InformationFragment informationFragment) {

        this.informationFragment = informationFragment;

    }
    public void findAllInformation(){

// 构造findAllInformation的tomcat服务请求URL

        String findAllInformationURL = MyHttpUtil.host + "/DeviceManage/findAllInformation";

        MyHttpUtil.sendOkhttpGetRequest(findAllInformationURL, new Callback() {

            public void onFailure(Call call, IOException e) {

                Log.i("zjc","web接口服务连接失败，请确保主机ip地址是否正确，然后 打开tomcat服务器");

                        MyAppliacation.subThreadToast("web接口服务连接失败，请确保主机ip地 址是否正确，然后打开tomcat服务器");

            }

            public void onResponse(Call call, Response response) throws IOException {

                parseJSONtoInformationList(response.body().string());

                informationFragment.showAllInformationCallback(informationListFromJson);

            }

        });

    }
}
